package yabandpay

import (
    "fmt"
	"time"
	"errors"
	"strconv"
	"encoding/json"
	"crypto/hmac"
    "crypto/sha256"
	"thirdpaysdk/core"
	"thirdpaysdk/utils/signature"
)

type Parameters struct {
	AppId     string `json:"appId"`
	NonceStr  string `json:"nonceStr"`
	Package   string `json:"package"`
	SignType  string `json:"signType"`
	TimeStamp string `json:"timeStamp"`
	PaySign   string `json:"paySign"`
}

type RspData struct {
	OrderId    string     `json:"order_id"`
	Amount     string     `json:"amount"`
	Currency   string     `json:"currency"`
	TradeId    string     `json:"trade_id"`
	Url        string     `json:"url"`
	Parameters Parameters `json:"parameters"`
	State      string     `json:"state"`
	Info       string     `json:"info"`
}

type MicroAppPayResponse struct {
	Result  string  `json:"result"`
	RspData RspData `json:"data"`
}

func ComputeHmac256(message string, secret string) string {
    key := []byte(secret)
    h := hmac.New(sha256.New, key)
    h.Write([]byte(message))
	bs := h.Sum(nil)
    result := fmt.Sprintf("%x", bs)
	return result
}

/*
{"result":"success","data":{"order_id":"2017020100002314","amount":"0.10","currency":"EUR","trade_id":"dcadf3af-2611-95b6-acbe-60c8ecddb3d4","url":"","parameters":{"appId":"wx54afe855ab7112e8","nonceStr":"b64c4feebdaa21fe18883e5a93ae1b66","package":"prepay_id=wx1413520487067206414c6d150235315781","signType":"HMAC-SHA256","timeStamp":"1547445125","paySign":"8E69FC78AE8704151FECC342153BC378C875B389222BB1F45A8DFE2D26D9EEDE"},"state":"processing","info":""}}
*/
func MicroAppPay(secreCode,orderId string,amount,currency,des,user,openId,
                  appId,notifyUrl string)(rsp *MicroAppPayResponse, err error){
	
	type ReqData struct {
		PayMethod    string  `json:"pay_method"`
		SubPayMethod string  `json:"sub_pay_method"`
		OrderId      string  `json:"order_id"`
		Amount       string  `json:"amount"`
		Currency     string  `json:"currency"`
		Description  string  `json:"description"`
		User         string  `json:"user"`
		AppId        string  `json:"app_id"`
		OpenId       string  `json:"open_id"`
		NotifyUrl    string  `json:"notify_url"`
		NonceStr     string  `json:"nonce_string"` 
		Timestamp    string  `json:"timestamp"` 
		  
	}
	type Request struct {
	    Sign    string  `json:"sign"`
		ReqData ReqData `json:"data"`
	}
	var request Request
	request.ReqData.PayMethod = "online"
	request.ReqData.SubPayMethod = "WeChatMiniPay"
	request.ReqData.OrderId = orderId
	request.ReqData.Amount = amount
	request.ReqData.Currency = currency
	request.ReqData.Description = des
	request.ReqData.User = user
	request.ReqData.AppId = appId
	request.ReqData.OpenId = openId
	request.ReqData.NotifyUrl = notifyUrl
	request.ReqData.NonceStr = signature.GetNonceStr(20)
	request.ReqData.Timestamp = strconv.FormatInt(time.Now().Unix()*1000, 10)
	signBody, e := json.Marshal(&request.ReqData)
	if e != nil {
	   err = e
	   return
	}
	request.Sign = ComputeHmac256(string(signBody),secreCode)
	completeURL := "https://api.yabandpay.com/payments.php"
	client := core.GetPayJSONClient("POST", completeURL)
	rsp = &MicroAppPayResponse{}
	_, e = client.PayJsonExcute(request, rsp)
    if e != nil {
		err = e
		return
	}
	if rsp.Result != "success" {
	   err = errors.New(rsp.Result+":"+rsp.RspData.Info)
	   return
	}
	return
}
